/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jims.master.modules.doctor.netHosptial.service;

import com.jims.master.common.service.CrudService;
import com.jims.master.modules.base.user.entity.MasterBaseinfo;
import com.jims.master.modules.base.user.service.MasterBaseinfoService;
import com.jims.master.modules.doctor.netHosptial.dao.NetPatMasterDao;
import com.jims.master.modules.doctor.netHosptial.entity.NetPatMaster;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 患者主记录Service
 * @author zp
 * @version 2016-05-31
 */
@Service
@Transactional(readOnly = true)
public class NetPatMasterService extends CrudService<NetPatMasterDao, NetPatMaster> {

	@Autowired
    private NetPatMasterDao netPatMasterDao;

    @Autowired
    private MasterBaseinfoService masterBaseinfoService;

    /**
     * 通过用户baseid获取病人基本信息
     * @param idNo
     * @return
     */
    public NetPatMaster getAll(String idNo){
        return  netPatMasterDao.getAll(idNo);
    }

    public List<NetPatMaster> selectAll (String idNo,String patient_id){
        List<NetPatMaster> patMasterIndexList=netPatMasterDao.selectAll(idNo,patient_id);
        return patMasterIndexList;
    }
    public List<NetPatMaster> selectClinicAll( NetPatMaster netPatMaster){
        return netPatMasterDao.selectClinicAll(netPatMaster);
    }

    public List<NetPatMaster> selectVisitAll( NetPatMaster netPatMaster){
        return netPatMasterDao.selectVisitAll(netPatMaster);
    }
    public NetPatMaster findIndexInfoByIdNo( NetPatMaster netPatMaster){
        return netPatMasterDao.findIndexInfoByIdNo(netPatMaster);
    }
    //通过身份证查询义诊患者
    public NetPatMaster findIndexFreeInfoByIdNo( NetPatMaster netPatMaster){
        return netPatMasterDao.findIndexFreeInfoByIdNo(netPatMaster);
    }
    //由IdNo初始化pat_master_index,先查询，不存在则新建数据
    //对于患者IdNo数据唯一性，有电话也保存但不作为数据关系
    /**
     * 通过用户baseid获取病人基本信息
     * @param-idNo
     * @param-name
     * @return
     */
    @Transactional(readOnly = false)
    public NetPatMaster initialIndexInfoByIdNo( NetPatMaster netPatMaster){
        //无身份证
        if(StringUtils.isEmpty(netPatMaster.getIdNo())){
            String patientId = netPatMasterDao.findMaxIndex();
            if(StringUtils.isEmpty(patientId)){
                patientId="CD1";
            }else{
                patientId="CD"+patientId;
            }
            netPatMaster.setPatientId(patientId);
            netPatMaster.setNamePhonetic("");
            netPatMaster.setChargeType("自费");
            netPatMaster.setIdentity("一般人员");
            netPatMaster.setBirthPlace("");
            netPatMaster.setVipIndicator(0);
            netPatMaster.setOperator("");
            netPatMaster.setUnitInContract("");
            netPatMaster.setRemarks("0");
            save(netPatMaster);
            return netPatMaster;

        }else{
            //由IdNo初始化pat_master_index,先查询，不存在则新建数据
            NetPatMaster sysEntity = netPatMasterDao.findIndexFreeInfoByIdNo(netPatMaster);
            if(sysEntity!=null){
                sysEntity.setRemarks("0");
                sysEntity.setIdNo(netPatMaster.getIdNo());
                sysEntity.setName(netPatMaster.getName());
                if(StringUtils.isNotEmpty(netPatMaster.getAge()))
                    sysEntity.setAge(netPatMaster.getAge());
                sysEntity.setSex(netPatMaster.getSex());
                sysEntity.setDateOfBirth(netPatMaster.getDateOfBirth());
                sysEntity.setPhoneNumberBusiness(netPatMaster.getPhoneNumberBusiness());
                save(sysEntity);
                return sysEntity;
            }else{
                //patientId
                String patientId = netPatMasterDao.findMaxIndex();
                if(StringUtils.isEmpty(patientId)){
                    patientId="CD1";
                }else{
                    patientId="CD"+patientId;
                }
                netPatMaster.setPatientId(patientId);
                //name、sex、dateOfBirth
                MasterBaseinfo baseInfo = null;
//                        masterBaseinfoService.findByCardNum(netPatMaster.getIdNo());
                if(baseInfo!=null){
                    String idcard = netPatMaster.getIdNo();
                    String sex;
                    String id17 = idcard.substring(16, 17);
                    if (Integer.parseInt(id17) % 2 != 0) {
                        sex = "男";
                    } else {
                        sex = "女";
                    }
                    if(StringUtils.isEmpty(baseInfo.getSex())){
                        netPatMaster.setSex(sex);
                    }else{
                        netPatMaster.setSex(baseInfo.getSex());
                    }
                    // 获取出生日期
                    String birthday ="";
                    birthday = idcard.substring(6, 14);
                    Date birthdate=null; ;
                    try {
                        birthdate = DateUtils.parseDate(birthday, "yyyyMMdd");
                    } catch (ParseException e) {
                        return null;
                    }
                    netPatMaster.setName(baseInfo.getName());
                    if(baseInfo.getBirthDate()!=null){
                        netPatMaster.setDateOfBirth(baseInfo.getBirthDate());
                    }else{
                        netPatMaster.setDateOfBirth(birthdate);
                    }
                    if(baseInfo.getBirthDate()==null||baseInfo.getSex()==""){
                        baseInfo.setBirthDate(birthdate);
                        baseInfo.setSex(sex);
                        masterBaseinfoService.save(baseInfo);
                    }
                }
                //name_phonetic，charge_type , identity , birth_place , vip_indicator ,
                // operator , unit_in_contract
                netPatMaster.setNamePhonetic("");
                netPatMaster.setChargeType("自费");
                netPatMaster.setIdentity("一般人员");
                netPatMaster.setBirthPlace("");
                netPatMaster.setVipIndicator(0);
                netPatMaster.setOperator("");
                netPatMaster.setUnitInContract("");
                netPatMaster.setRemarks("0");
                save(netPatMaster);
                return netPatMaster;
            }
        }
    }

    //改变发送标志
    @Transactional(readOnly = false)
    public Integer send(NetPatMaster netPatMaster){
        return dao.send(netPatMaster);
    }

    /**
     * 义诊患者不关联检查检验信息
     * @return
     * @author xueyx
     */
    public List<NetPatMaster> onlyFreePersonList(NetPatMaster netPatMaster){
        return dao.onlyFreePersonList(netPatMaster);
    };
    /**
     * 通过用户patientId获取病人基本信息
     * @return
     * @author xueyx
     */
    public NetPatMaster findIndexInfoByPatientId(String patientId){
        return  dao.findIndexInfoByPatientId(patientId);
    }
    /**
     * 通过用户用户baseinfo获取病人基本信息
     * @return
     * @author xueyx
     */
    public NetPatMaster findIndexInfoByBaseinfo(String baseinfo){
        return  dao.findIndexInfoByBaseinfo(baseinfo);
    }

    /**
     * 旧虚拟诊室有检测检验的患者
     * @return
     * @author xueyx
     */
    public List<NetPatMaster> findXuniPatMasterIndexs(NetPatMaster netPatMaster){
        return dao.findXuniPatMasterIndexs(netPatMaster);
    }
    /**
     * 新虚拟诊室有检测检验的患者
     * @return
     * @author xueyx
     */
    public List<NetPatMaster> findResultPatMasterIndexs(NetPatMaster netPatMaster){
        return dao.findResultPatMasterIndexs(netPatMaster);
    }

    public List<NetPatMaster> findIndexInfoByIdNoOrTel( String idNo, String tel){
        return dao.findIndexInfoByIdNoOrTel(idNo, tel);
    }

}